<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>农资管理</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        body { background-color: #f5f7fa; }
        .navbar { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
        .sidebar { min-height: calc(100vh - 56px); background: white; box-shadow: 2px 0 5px rgba(0,0,0,0.1); padding-top: 20px; }
        .sidebar .nav-link { color: #333; padding: 12px 20px; border-left: 3px solid transparent; }
        .sidebar .nav-link:hover { background-color: #f8f9fa; border-left-color: #667eea; }
        .sidebar .nav-link.active { background-color: #e7f1ff; border-left-color: #667eea; color: #667eea; }
        .content-wrapper { background: white; border-radius: 10px; padding: 25px; margin: 20px; }
        .btn-primary { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border: none; }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/index">🌾 智慧农业管理系统</a>
            <div class="navbar-nav ms-auto">
                <span class="navbar-text text-white me-3" th:if="${session.currentUser}">欢迎，<span th:text="${session.currentUser.realName}"></span></span>
                <a class="btn btn-outline-light btn-sm" href="/logout">退出登录</a>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <nav class="col-md-2 d-md-block sidebar">
                <ul class="nav flex-column">
                    <li class="nav-item"><a class="nav-link" href="/index"><i class="fas fa-home me-2"></i>首页</a></li>
                    <li class="nav-item" th:if="${session.currentUser.role == 'admin'}"><a class="nav-link" href="/user/list"><i class="fas fa-users me-2"></i>用户管理</a></li>
                    <li class="nav-item"><a class="nav-link" href="/field/list"><i class="fas fa-map me-2"></i>地块管理</a></li>
                    <li class="nav-item"><a class="nav-link" href="/crop/list"><i class="fas fa-seedling me-2"></i>作物管理</a></li>
                    <li class="nav-item"><a class="nav-link active" href="/material/list"><i class="fas fa-boxes me-2"></i>农资管理</a></li>
                    <li class="nav-item"><a class="nav-link" href="/plan/list"><i class="fas fa-calendar-alt me-2"></i>种植计划</a></li>
                    <li class="nav-item"><a class="nav-link" href="/farming/list"><i class="fas fa-tractor me-2"></i>农事记录</a></li>
                    <li class="nav-item"><a class="nav-link" href="/monitor/list"><i class="fas fa-chart-line me-2"></i>生长监测</a></li>
                    <li class="nav-item"><a class="nav-link" href="/product/list"><i class="fas fa-shopping-basket me-2"></i>产品库存</a></li>
                    <li class="nav-item"><a class="nav-link" href="/sales/list"><i class="fas fa-dollar-sign me-2"></i>销售记录</a></li>
                    <li class="nav-item"><a class="nav-link" href="/statistics/index"><i class="fas fa-chart-bar me-2"></i>数据统计</a></li>
                    <li class="nav-item"><a class="nav-link" href="/news/list"><i class="fas fa-newspaper me-2"></i>新闻管理</a></li>
                    <li class="nav-item"><a class="nav-link" href="/news/index"><i class="fas fa-bullhorn me-2"></i>公告浏览</a></li>
                </ul>
            </nav>

            <main class="col-md-10">
                <div class="content-wrapper">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h3><i class="fas fa-boxes me-2"></i>农资管理</h3>
                        <button class="btn btn-primary"><i class="fas fa-plus me-2"></i>新增农资</button>
                    </div>

                    <div th:if="${materials != null and !materials.isEmpty()}">
                        <table class="table table-hover">
                            <thead>
                                <tr><th>ID</th><th>名称</th><th>类型</th><th>规格</th><th>库存</th><th>单价</th><th>供应商</th><th>操作</th></tr>
                            </thead>
                            <tbody>
                                <tr th:each="m : ${materials}">
                                    <td th:text="${m.id}"></td>
                                    <td th:text="${m.materialName}"></td>
                                    <td>
                                        <span class="badge bg-success" th:if="${m.materialType == 'fertilizer'}">化肥</span>
                                        <span class="badge bg-primary" th:if="${m.materialType == 'seed'}">种子</span>
                                        <span class="badge bg-warning" th:if="${m.materialType == 'pesticide'}">农药</span>
                                    </td>
                                    <td th:text="${m.specification}"></td>
                                    <td><span th:text="${m.quantity}"></span> <span th:text="${m.unit}"></span></td>
                                    <td>¥<span th:text="${m.price}"></span></td>
                                    <td th:text="${m.supplier}"></td>
                                    <td>
                                        <button class="btn btn-sm btn-primary"><i class="fas fa-edit"></i></button>
                                        <button class="btn btn-sm btn-danger" th:onclick="'deleteMaterial(' + ${m.id} + ')'"><i class="fas fa-trash"></i></button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div th:if="${materials == null or materials.isEmpty()}" class="alert alert-info text-center">暂无农资数据</div>
                </div>
            </main>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
    <script>
        function deleteMaterial(id) {
            if (confirm('确定删除？')) {
                $.post('/material/delete/' + id, function(res) {
                    if (res.code === 200) { alert('删除成功'); location.reload(); }
                    else { alert(res.message); }
                });
            }
        }
    </script>
</body>
</html>

